function mouseoverListener(event){
		var self=event.target;
		if(self==event.target)
		{
			if (self.innerText != "公共信息")
				return;
			var tag=$(self)[0].tagName.toLowerCase();
			var msgarr=new Array();
			//console.log(self);
			var currentURL =document.URL.split('?')[0];
			msgarr[0]=parseDomNode(self,tag,"mouseover",currentURL);
			var node=self;
			var domarr=new Array();
			var nodeTag="";
			var nodeContent="";
			var nodeClass="";
			var nodeId="";
			var nodeTitle="";
			var nodeSrc="";
			var nodeHref="";
			if(node.tagName!=undefined)
				nodeTag=node.tagName;
			if(node.innerText!=undefined)
				nodeContent=node.innerText;
			if(node.className!=undefined)
				nodeClass=node.className;
			if(node.id!=undefined)
				nodeId=node.id;
			if(node.title!=undefined)
				nodeTitle=node.title;
			if(node.src!=undefined)
				nodeSrc=node.src;
			if(node.href!=undefined)
				nodeHref=encodeURIComponent(node.href);
			domarr[0]=new Array();
			//console.log(msgarr[0].contextValue);
			domarr[0].push('<dom> <tagname>'+nodeTag+'</tagname> <content>'+msgarr[0].contextValue+'</content> <class>'+nodeClass+'</class> <id>'+nodeId+'</id> <title>'+nodeTitle+'</title> <src>'+nodeSrc+'</src> <href>'+nodeHref+'</href></dom>');
			var nodeIndex=new Array();
			var nodeNames=new Array();
			var nodeContext=new Array();
			if($(node).parent().parent().parent().length>0)
			{
				var greatFather=$(node).parent().parent().parent()[0];
				nodeContext.push(greatFather.innerText);
			}
			while(node.tagName!='HTML')
			{
				var father=$(node).parent()[0];
				nodeIndex.push($(node).index());
				nodeNames.push(node.tagName);
				node=father;
			}
			domarr[1]="<dom>"+nodeIndex+"</dom>";
			domarr[2]="<dom>"+nodeNames+"</dom>";
			domarr[3]="<dom>"+encodeURIComponent(nodeContext)+"</dom>";
			//console.log(domarr);
			msgarr[1]=domarr;
			chrome.extension.sendMessage(msgarr, function(response) {//console.log(response['word']);
			});
		}
}
function clickListener(event){
		var self=event.target;
		//console.log(event.target);
		//console.log(self);
		if(self==event.target)
		{
			var tag=$(self)[0].tagName.toLowerCase();
			//event.stopPropagation(); 
			var msgarr=new Array();
			//console.log(self);
			var currentURL = document.URL.split('?')[0];
			msgarr[0]=parseDomNode(self,tag,"click",currentURL);
			var node=self;
			var domarr=new Array();
			var nodeTag="";
			var nodeContent="";
			var nodeClass="";
			var nodeId="";
			var nodeTitle="";
			var nodeSrc="";
			var nodeHref="";
			if(node.tagName!=undefined)
				nodeTag=node.tagName;
			if(node.innerText!=undefined)
				nodeContent=node.innerText;
			if(node.className!=undefined)
				nodeClass=node.className;
			if(node.id!=undefined)
				nodeId=node.id;
			if(node.title!=undefined)
				nodeTitle=node.title;
			if(node.src!=undefined)
				nodeSrc=node.src;
			if(node.href!=undefined)
				nodeHref=encodeURIComponent(node.href);
			domarr[0]=new Array();
			domarr[0].push('<dom> <tagname>'+nodeTag+'</tagname> <content>'+msgarr[0].contextValue+'</content> <class>'+nodeClass+'</class> <id>'+nodeId+'</id> <title>'+nodeTitle+'</title> <src>'+nodeSrc+'</src> <href>'+nodeHref+'</href></dom>');
			var nodeIndex=new Array();
			var nodeNames=new Array();
			var nodeContext=new Array();
			if($(node).parent().parent().parent().length>0)
			{
				var greatFather=$(node).parent().parent().parent()[0];
				//console.log(greatFather.innerText);
				nodeContext.push(greatFather.innerText);
			}
			while(node.tagName!='HTML')
			{
				var father=$(node).parent()[0];
				nodeIndex.push($(node).index());
				nodeNames.push(node.tagName);
				node=father;
			}
			domarr[1]="<dom>"+nodeIndex+"</dom>";
			domarr[2]="<dom>"+nodeNames+"</dom>";
			domarr[3]="<dom>"+encodeURIComponent(nodeContext)+"</dom>";
			//console.log(domarr);
		//domarr.reverse();
			/*if(window.document.URL!=currentURL)
			{
				domarr[0]=1;
				if(document.isSameNode(event.srcElement.ownerDocument))
				{
					msgarr[0].url=document.URL;
				}
				else{
					return;
				}
			}
			else
			{
				domarr[0]=0;
			}
			*/
		//console.log(domarr);
		msgarr[1]=domarr;
		chrome.extension.sendMessage(msgarr, function(response) {//console.log(response['word']);
		});
	}
}
function keyupListener(event){
		var self=event.target;
		if(self==event.target)
		{
			var tag=$(self)[0].tagName.toLowerCase();
			var msgarr=new Array();
			//console.log(self);
			var currentURL =document.URL.split('?')[0];
			msgarr[0]=parseDomNode(self,tag,"edit",currentURL);
			var node=self;
			var domarr=new Array();
			var nodeTag="";
			var nodeContent="";
			var nodeClass="";
			var nodeId="";
			var nodeTitle="";
			var nodeSrc="";
			var nodeHref="";
			if(node.tagName!=undefined)
				nodeTag=node.tagName;
			if(node.innerText!=undefined)
				nodeContent=node.innerText;
			if(node.className!=undefined)
				nodeClass=node.className;
			if(node.id!=undefined)
				nodeId=node.id;
			if(node.title!=undefined)
				nodeTitle=node.title;
			if(node.src!=undefined)
				nodeSrc=node.src;
			if(node.href!=undefined)
				nodeHref=encodeURIComponent(node.href);
			domarr[0]=new Array();
			//console.log(msgarr[0].contextValue);
			domarr[0].push('<dom> <tagname>'+nodeTag+'</tagname> <content>'+msgarr[0].contextValue+'</content> <class>'+nodeClass+'</class> <id>'+nodeId+'</id> <title>'+nodeTitle+'</title> <src>'+nodeSrc+'</src> <href>'+nodeHref+'</href></dom>');
			var nodeIndex=new Array();
			var nodeNames=new Array();
			var nodeContext=new Array();
			if($(node).parent().parent().parent().length>0)
			{
				var greatFather=$(node).parent().parent().parent()[0];
				nodeContext.push(greatFather.innerText);
			}
			while(node.tagName!='HTML')
			{
				var father=$(node).parent()[0];
				nodeIndex.push($(node).index());
				nodeNames.push(node.tagName);
				node=father;
			}
			domarr[1]="<dom>"+nodeIndex+"</dom>";
			domarr[2]="<dom>"+nodeNames+"</dom>";
			domarr[3]="<dom>"+encodeURIComponent(nodeContext)+"</dom>";
			//console.log(domarr);
			msgarr[1]=domarr;
			chrome.extension.sendMessage(msgarr, function(response) {//console.log(response['word']);
			});
		}
}
	var LevenshteinDistance = {
			_str1:null,
			_str2:null,
			_matrix:null,
			_isString:function(s){
				return Object.prototype.toString.call(s) === '[object String]';
			},
			_isNumber:function(s){
				return Object.prototype.toString.call(s) === '[object Number]';
			},
			init:function(str1,str2){
					this._str1 = str1;
					this._str2 = str2;
				if(str1==null || str2==null) 
					return this;
				if(str1.length==0)
					this._str1=null;
				if(str2.length==0)
					this._str2=null;
				str1.length &&  str2.length && this._createMatrix(str1.length+1,str2.length+1);
				this._matrix && this._initMatrix();
				return this;
			},
			get:function(flag){
				if(this._str1==null&&this._str2==null)
					return 0;
				if(this._str1==null||this._str2==null)
					return 1;
				return this._getDistance(flag)/Math.max(this._str1.length,this._str2.length);
			},
			_getDistance:function(flag){
				var len1 = this._str1.length,
					len2 = this._str2.length;

				if(!len1 || !len2) return Math.max(len1,len2);

				var str1 = this._str1,//.split(''),
					str2 = this._str2;//.split('');

				var i = 0,j = 0,temp = 0;
				while(i++ < len1){
					j = 0;
					while(j++ < len2){
						 temp = this._myCompare(str1[i-1],str2[j-1],flag) ? 0 : 1;
						 this._matrix[i][j] = Math.min(this._matrix[i-1][j]+1,this._matrix[i][j-1]+1,this._matrix[i-1][j-1]+temp);
					 }
				 }
				 return this._matrix[i-1][j-1];
			 },
			 _initMatrix:function(){
				 var cols = this._matrix[0].length,
					 rows = this._matrix.length;
				 var l = Math.max(cols,rows);
				 while(l--){
					 cols-1 >= l && (this._matrix[0][l] = l);
					 rows-1 >= l && (this._matrix[l][0] = l);
				}
			},
			_createMatrix:function(n,m){
				if(!this._isNumber(n) || !this._isNumber(m) || n<1 || m<1) return;

				this._matrix = new Array(n),i = 0;
				while(i<n) this._matrix[i++] = new Array(m);
			},
			_myCompare:function(str1,str2,flag)
			{
				if(flag==true)
				{
					if(computeDis(str1, str2,true)<350)
						return true;
					else
						return false;
				}
				else
				{
					return str1==str2;
				}
				
			}
		
	}

function computeDis(srcNode,tarNode,flag)
{
	 var srcNode=srcNode.split(/<\/\S+>/g);
	 var srcTag=srcNode[0].replace(/<dom> <tagname>/g,"").replace(/<tagname>/g,"");
	 var srcContent=srcNode[1].replace(/ <content>/g,"");
	 var srcClass=srcNode[2].replace(/ <class>/g,"");
	 var srcId=srcNode[3].replace(/ <id>/g,"");
	 var srcTitle=srcNode[4].replace(/ <title>/g,"");
	 var srcSrc=srcNode[5].replace(/ <src>/g,"");
	 var srcHref=srcNode[6].replace(/ <href>/g,"");
	 if(!flag)
	 {
		 var tarTag="";
		 var tarContent="";
		 var tarClass="";
		 var tarId="";
		 var tarTitle="";
		 var tarSrc="";
		 var tarHref="";
			if(tarNode.tagName!=undefined)
				tarTag=tarNode.tagName;
			if(tarNode.innerText!=undefined)
				tarContent=tarNode.innerText;
			if(tarNode.className!=undefined)
				tarClass=tarNode.className;
			if(tarNode.id!=undefined)
				tarId=tarNode.id;
			if(tarNode.title!=undefined)
				tarTitle=tarNode.title;
			if(tarNode.src!=undefined)
				tarSrc=tarNode.src;
			if(tarNode.href!=undefined)
				tarHref=tarNode.href;
		}
		else
		{
			 var tarNode=tarNode.split(/<\/\S+>/g);
			 var tarTag=tarNode[0].replace(/<dom> <tagname>/g,"").replace(/<tagname>/g,"");
			 var tarContent=tarNode[1].replace(/ <content>/g,"");
			 var tarClass=tarNode[2].replace(/ <class>/g,"");
			 var tarId=tarNode[3].replace(/ <id>/g,"");
			 var tarTitle=tarNode[4].replace(/ <title>/g,"");
			 var tarSrc=tarNode[5].replace(/ <src>/g,"");
			 var tarHref=tarNode[6].replace(/ <href>/g,"");
		}
		
	 var tagDis=LevenshteinDistance.init(srcTag,tarTag).get(false)*100;
	 var contentDis=LevenshteinDistance.init(srcContent,tarContent).get(false)*100;
	 var classDis=LevenshteinDistance.init(srcClass,tarClass).get(false)*100;
	 var idDis=LevenshteinDistance.init(srcId,tarId).get(false)*100;
	 var titleDis=LevenshteinDistance.init(srcTitle,tarTitle).get(false)*100;
	 var srcDis=LevenshteinDistance.init(srcSrc,tarSrc).get(false)*100;
	 var hrefDis=LevenshteinDistance.init(srcHref,tarHref).get(false)*100;
	 var totalDis=parseInt(tagDis+contentDis+classDis+idDis+titleDis+srcDis+hrefDis);
	 return totalDis;
}
function computeContentDis(srcNode,tarNode,optype,srcType)
{
	 var srcNode=srcNode.split(/<\/\S+>/g);
	 var srcContent=srcNode[1].replace(/ <content>/g,"");
	 var tarContent="";
	 var tag=$(tarNode)[0].tagName.toLowerCase();
	 var parseResult=parseDomNode(tarNode,tag,optype,"");
	 var tarType=parseResult.type;
	 if (tarType.indexOf('an image')>=0)
	 	tarContent=decodeURIComponent(parseResult.contextValue);
	 else
	 	tarContent=parseResult.contextValue;
	 if (srcType.indexOf('an image')>=0)
	 	srcContent=decodeURIComponent(srcContent);
	 //console.log(tarContent.length);
	 //console.log(srcContent.length);
	 // console.log(tarContent);
	 // console.log(srcContent);
	 var contentDis=LevenshteinDistance.init(srcContent,tarContent).get(false)*100;
	 //console.log(contentDis);
	 return contentDis;
}
function computeFatherDis(nodeIndex,nodeNames,nodeContext,tarNode)
{
	var count=0;
	var node=tarNode;
	var tarIndex=new Array();
	var tarNames=new Array();
	var tarContext=new Array();
	if($(node).parent().parent().parent().length>0)
	{
		var greatFather=$(node).parent().parent().parent()[0];
		tarContext.push(greatFather.innerText);
	}
	while(node.tagName!='HTML')
	{
		var father=$(node).parent()[0];
		tarIndex.push($(node).index());
		tarNames.push(node.tagName);
		node=father;
	}
	 var indexDis=LevenshteinDistance.init(nodeIndex,tarIndex.toString()).get(false)*100;
	 var nameDis=LevenshteinDistance.init(nodeNames,tarNames.toString()).get(false)*100;
	 var contextDis=LevenshteinDistance.init(nodeContext,tarContext.toString()).get(false)*100;
	 var totalDis=((indexDis+nameDis+contextDis)/300)*700;
	// console.log(totalDis);
	return parseInt(totalDis);
	
}
function findContextValue(domNode)
{
	var found=false;
	var result="";
	if(domNode.innerText.length>0)
		return domNode.innerText;
	while(domNode.tagName!="FORM")
	{
		var preSib=domNode.previousSibling;
		var nextSib=domNode.nextSibling;
		while(preSib!=null||nextSib!=null)
		{
			var preText="";
			//console.log(preSib);
			//console.log(nextSib);
			
			
			if(preSib!=null&&preSib.nodeName.toLowerCase()!="script")
			{
				//console.log(preSib.nodeName);
				if(preSib.nodeName== '#text')
				{
					preText=preSib.nodeValue.trim();
				}
				else
				{
					if(preSib.innerText!=null)
						preText=preSib.innerText.trim();
				}
				//console.log(preText);
			}
			var nextText="";
			if(nextSib!=null&&nextSib.nodeName.toLowerCase()!="script")
			{
				//console.log(nextSib.nodeName);
				if(nextSib.nodeName== '#text')
				{
					nextText=nextSib.nodeValue.trim();
				}
				else
				{
					if(nextSib.innerText!=null)
						nextText=nextSib.innerText.trim();
				}
				//console.log(nextText);
			}
			if(preText.length>0)
			{
				found=true;
				result=preText;
				return result;
			}
			if(nextText.length>0)
			{
				found=true;
				result=nextText;
				return result;
			}
			if(preSib!=null)
				preSib=preSib.previousSibling;
			if(nextSib!=null)
				nextSib=nextSib.nextSibling;
		}
		domNode=domNode.parentNode;
	}
	return result;
}
function parseDomNode(node,tagname,optype,currentURL)
{
	var self=node;
	var answer;
	var reg=/^h[0-9]/;
	switch(optype)
	{
		case "mouseover":
		case "click":
		switch (tagname)
			{
			case 'a':
				var v=self.innerText;
				answer={type:'a link',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'body':
				answer={type:'the body of the page',value:"",url:currentURL,operation:optype,contextValue:v};
				break;
			case 'button':
				var v=self.innerText;
				answer={type:'a button',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'div':
				var v1=$(self).attr("id");
				var v2=$(self).attr("name");
				var v;
				if(v1&&v1.length>0)
				{
					v=v1;
				}
				else
				{
					if(v2&&v2.length>0)
					{
						v=v2;
					}
					else
						v="";
				}
				answer={type:'a div',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'em':
				var v=self.innerText;
				answer={type:'an emphasized text',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'form':
				var v1=$(self).attr("id");
				var v2=$(self).attr("name");
				var v;
				if(v1&&v1.length>0)
				{
					v=v1;
				}
				else
				{
					if(v2&&v2.length>0)
					{
						v=v2;
					}
					else
						v="";
				}
				answer={type:'a form',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case reg.test(tagname):
				var v=self.innerText;
				answer={type:'a highlighted text',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'img':
				//console.log(self.src);
				//console.log($(self).attr("src"));
				var v=encodeURIComponent(self.src);
				answer={type:'an image',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'input':
				var v;
				var type = $(self).attr("type");
				switch(type)
				{
					case "text":
					{
						v=findContextValue(self);
						answer={type:'a text input',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
					case "password":
					{
						v=findContextValue(self);
						answer={type:'a password input',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
					case "file":
					{
						v="";
						answer={type:'a file input',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
					case "image":
					{
						v=encodeURIComponent(self.src);
						answer={type:'an image input',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
					case "reset":
					{
						v=$(self).attr("value");
						answer={type:'a reset button',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
					case "radio":
					{
						v=findContextValue(self);
						answer={type:'a radio',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
					case "checkbox":
					{
						v=findContextValue(self);
						answer={type:'a checkbox',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
					case "button":
					{
						v=$(self).attr("value");
						if(v&&v.length==0)
						{
							v="";
						}
						answer={type:'a button',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
						
					}
					case "submit":
					{
						v=$(self).attr("value");
						if(v&&v.length==0)
						{
							v="";
						}
						answer={type:'a submit button',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
						
					}
					default:
					{
						v=findContextValue(self);
						answer={type:'an input',value:v,url:currentURL,operation:optype,contextValue:v};
						break;
					}
				}
				break;
			case 'label':
				var v=self.innerText;
				answer={type:'a label',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'li':
				var v=self.innerText;
				answer={type:'an item from a list',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'p':
				var v=self.innerText;
				answer={type:'a text paragraph',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'table':
				var v=$(self).attr("name");
				answer={type:'a table',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'tbody':
				var v="";
				answer={type:'the body of a table',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			case 'select':
				var v0=self.selectedIndex;
				var v1="";
				var v2="";
				if(v0>=0)
				{
					//console.log(self.options[v0].textContent);
					v1=self.options[v0].textContent;
				}
				for (var i=0;i<self.options.length;i++)
				{
					v2=v2+self.options[i].textContent;
				}
				//console.log(v2);
				answer={type:'single-select',value:v1,url:currentURL,operation:optype,contextValue:v2};
				break;
			case 'option':
				var v1="";
				var v2="";
				var parent = $(self).parent();
				while( $(parent)[0].tagName.toLowerCase() != "select")
					parent = $(parent).parent();
				var Options = parent[0].options;
				for (var i=0; i<Options.length; i++)
				{
					v1 = v1+Options[i].textContent;
					if(Options[i].selected==true)
					{
						if(v2=="")
							v2=Options[i].textContent;
						else
							v2=v2+","+Options[i].textContent;
					}
				}
				//console.log(v1);
				//console.log(v2);
				answer={type:'multi-select',value:v2,url:currentURL,operation:optype,contextValue:v1};
				break;
			case 'textarea':
				var v=$(self).attr('value');
				answer={type:'textarea',value:v,url:currentURL,operation:optype,contextValue:v};
				break;
			default :
				var v=self.innerText;
				answer={type:tagname,value:v,url:currentURL,operation:optype,contextValue:v};
			}
		break;
		
		case "edit":
			switch (tagname)
			{
			case 'input':
				var v=$(self).attr('value');
				var type = $(self).attr('type');
				var contextv=findContextValue(self);
				//console.log(contextv);
				//console.log(contextv.length);
				if ( type == "password" )
				{
					answer={type:'password',value:v,url:currentURL,operation:"edit",contextValue:contextv};
				}
				else
				{
					answer={type:'input',value:v,url:currentURL,operation:"edit",contextValue:contextv};
				}
				break;
			case 'textarea':
				var contextv=findContextValue(self);
				var v=$(self).attr('value');
				answer={type:tagname,value:v,url:currentURL,operation:"edit",contextValue:contextv};
				break;
			default:
				var v = event.which;
				answer={type:'a key',value:v,url:currentURL, operation:"press"};
			}
		break;
	}
	return answer;
}
//$("*").live('click',clickListener);
//$("*").live('keyup',keyupListener);
function getAllFramesNum(frame) {
	var len = frame.frames.length;
	var sum = len;
	for (var i = 0; i < len; ++i)
		sum += getAllFramesNum(frame.frames[i]);
	return sum;
}
window.addEventListener('click',clickListener,true);
window.addEventListener('keyup',keyupListener,true);
//window.addEventListener('mouseover',mouseoverListener,true);
chrome.runtime.onConnect.addListener(function(port) {
	console.assert(port.name == "replay");
	console.log('Connected! ' + window.location.href + ' ' + document.URL);
	port.onMessage.addListener(function(request) {
		console.log('Message recieved!');
		if (request == 'abort') {
			//alert('Fail to replay the script!');
			return;
		}
		var opt=request.Operation;
		var domarray=request.DomArray;
		var sum = getAllFramesNum(top) + 1;
		if(document.URL.split(':')[0]=='file')
		{
			domarray=request.DomArray[1].split(',');
			console.log("offline replay!");
			var node;
			domarray.reverse();
			node=document.documentElement;
			for(var i=0;i<domarray.length;i++)
			{
				if(node.children[parseInt(domarray[i])]==null) {
					console.log('not found!' + document.URL);
					port.postMessage({result: "fail", url: document.URL, framesNum: sum});
					return;
				}
				var child=node.children[parseInt(domarray[i])];
				node=child;
			}
			/* console.log(node);
			$(node).css("background-color","yellow");
			$(node).css("color","green");
			$(node).css("background","yellow");
			*/
		}
		else
		{
			//  if(request.Url!=document.URL.split("?")[0] && request.Url!=decodeURIComponent(document.URL.split("?")[0]))
			//  {
			// console.log("not correct url!\n" + "request url: " + request.Url + "\nnow url: " + document.URL.split("?")[0] + "\n");
			// return;
			//  }
			//console.log(opt);

			var srcNode=domarray[0];
			var nodeIndex=domarray[1];
			var nodeNames=domarray[2];
			var nodeContext=decodeURIComponent(domarray[3]);
			//comparison for lowest node:

			var targetTag=srcNode.split(/<\/\S+>/g)[0].replace(/<dom> <tagname>/g,"");
			var targetNodes=$(targetTag);
			var perfectNodes=new Array();
			var leafDis=new Array();
			var fatherTag=true;
			//console.log(targetNodes);
			//first filter
			var count=0;
			var resultNodes=new Array();
			$(targetNodes).each(function(index,ele) {
				var tempDis=computeContentDis(srcNode,ele,opt,request.Tag);
				if(tempDis<=30)
				{
					resultNodes.push(ele);
					leafDis[count]=tempDis;
					count++;
					if(tempDis==0)
					{
						perfectNodes.push(ele);
					}
				}
			});
			targetNodes=resultNodes;
			if(perfectNodes.length>0)
			{
				if(perfectNodes.length==1)
				{
					node=perfectNodes[0];
					fatherTag=false;
				}
				else
				{
					//second filter
					targetNodes=perfectNodes;
					perfectNodes=new Array();
					var count=0;
					leafDis=new Array();
					$(targetNodes).each(function(index,ele){
						var tempDis=computeDis(srcNode,ele,false);
						if(tempDis>300)
						{
							targetNodes.splice(index,1);	
						}
						else
						{
							leafDis[count]=tempDis;
							count++;
							if(tempDis==0)
							{
								perfectNodes.push(ele);
							}
						}
					});
					if(perfectNodes.length>0)
					{
						if(perfectNodes.length==1)
						{
							node=perfectNodes[0];
							fatherTag=false;
						}
						else
						{
							targetNodes=perfectNodes;
						}
					}
				}
			}
			//third filter
			if(fatherTag)
			{
			  var minDis=1400;
			  var minDisNode;
			  console.log(targetNodes);
			  $(targetNodes).each(function(index,ele) {
				var fatherDis=computeFatherDis(nodeIndex,nodeNames,nodeContext,ele);
				var totalDis=leafDis[index]+fatherDis;
				console.log(totalDis);
				if(totalDis<minDis)
				{
					minDis=totalDis;
					minDisNode=ele;
				}
			});	
			//if(minDis>400)
			//	return;
			var node=minDisNode;
			}
		}
		console.log(node);
		//console.log(perfectNodes);
		if (node==null) {
			console.log('not found!' + document.URL);
			port.postMessage({result: "fail", url: document.URL, framesNum: sum});
			return;
		}

		$(node).css("background-color","yellow");
		$(node).css("color","green");
		$(node).css("background","yellow");
		switch (opt)
		{
			case 'click':
				//setTimeout(function(){node.click();},1000);
				switch(request.Tag)
				{
					/*case 'a link':
						//setTimeout(function(){node.click();},1000);
						setTimeout(function(){node.click();},0);
						console.log($(node).parents('form'));
						break;
					case 'a button':
						//setTimeout(function(){node.click();},1000);
						setTimeout(function(){node.click();},0);
						console.log($(node).parents('form'));
						break;
					*/
					case 'single-select':
						//var children = node.children;
						var selectedItem = request.Value;
						//console.log(selectedItem);
						var j=0;
						for (var i=0; i<node.options.length; i++)
						{
							var selectStr = $(node.options[i]).attr("selected");
							if ( node.options[i].textContent==selectedItem )
							{
								$(node.options[i]).attr("selected", true);
							}
							else
							{
								if (selectStr!="")
								{
									$(node.options[i]).removeAttr("selected");
								}
							}
						}
						break;
					case 'multi-select':
						//var children = node.parentNode.children;
						var parent=node.parentNode;
						var selectedItems = request.Value.split(",");
						//console.log(selectedItems);
						//console.log(parent.options);
						var j=0;
						for (var i=0; i<parent.options.length; i++)
						{
							var selectStr = $(parent.options[i]).attr("selected");
							var item="";
							if ( j<selectedItems.length )
								item = selectedItems[j];
							//console.log(parent.options[i].textContent);
							//console.log(item);
							if ( parent.options[i].textContent==item )
							{
								//console.log(item);
								$(parent.options[i]).attr("selected", true);
								j++;
							}
							else
							{
								if (selectStr!="")
								{
									$(parent.options[i]).removeAttr("selected");
								}
							}
						}
						break;
					case 'a checkbox':
						//console.log(node.checked);
						if(node.checked==true)
						{
							//console.log("true");
							node.checked=false;
						}
						else
						{
							//console.log("false");
							node.checked=true;
						}
						break;
					/*case 'a radio':
						var node=document.childNodes[0];
						var Index=nodeIndex.split(",");
						Index=Index.reverse();
						console.log(node);
						for (var i=0;i<Index.length;i++)
						{
							console.log(Index[i]);
							node=node.children[parseInt(Index[i])];
							console.log(node);
						}
						console.log(node);
						node.click();
						break;
						
					*/
					default:
						if (node.click)
							node.click();
						else {
							try {
							var a = document.createEvent("MouseEvents");
							a.initEvent("click", true, true);
							node.dispatchEvent(a);
							} catch (d) {
								alert(d);
							}
						}
						break;
				}
				break;
			case 'edit':
				$(node).attr("value",request.Value);
				break;
			case 'press':
				break;
			case 'mouseover':
				try {
					var a = document.createEvent("HTMLEvents");
					a.initEvent("mouseover", true, true);
					node.dispatchEvent(a);
				} catch (d) {
					alert(d);
				}
				break;
		}
		console.log('success!' + document.URL);
		port.postMessage({result: "success", url: document.URL, framesNum: sum});
	});
});